package com.hula.onion.book.server.spider.platforms.shitoujinrong;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.SpiderCommon;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.utils.HttpUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Map;

/**
 * Created by work on 2018/1/24.
 */
@Component("shiTouSpider")
public class ShiTouSpider extends SpiderCommon {

    @Autowired
    private ShiTouSpiderHelper spiderHelper;

    private static final String LOGIN_URL = "https://www.shitou.com/api-proxy/v4/user/login.do";

    private static final String ACCOUNT_INFO = "https://www.shitou.com/api-proxy/v4/myAccount/base.do";

    @Override
    public CommonResponse execute() {
        HttpClientContext httpContext = new HttpClientContext();
        BasicCookieStore cookieStore = new BasicCookieStore();
        httpContext.setCookieStore(cookieStore);
        Map<String, Object> param = spiderHelper.installRequestParams(loginForm);
        BasicHeader bh = new BasicHeader("Cookie", "username="+loginForm.getUserName());
        String loginStr = HttpUtils.post(LOGIN_URL, param, httpContext,bh);
        if (StringUtils.isEmpty(loginStr)) {
            return new CommonResponse(ResponseStatusEnum.FAIL);
        }
        JSONObject loginJson = JSON.parseObject(loginStr);
        System.out.println(loginJson);
        if ("100001".equals(loginJson.getString("code"))) {
            return new CommonResponse(ResponseStatusEnum.ACCOUNT_ERROR);
        }
        if ("000000".equals(loginJson.getString("code"))) {
            Map<String, Object> params = Collections.emptyMap();
            String s="username=15510252326; aliyungf_tc=AQAAAHiVf2VxgQ0A/BmMPbbQfHcyf9rm; acw_tc=AQAAADF+6WhjMg4A/BmMPU5QAXdUAJ14; id=491622; token=51fe507e-3ce9-4665-b256-8d872f5cefcb; name=%25E9%2599%2588%25E5%258D%2593; userType=0; firstLogin=1; sid=";
            for (Cookie c:cookieStore.getCookies()){
                System.out.println(c.getName()+":"+c.getValue());
                if("sid".equals(c.getName())){
                    s+=c.getValue();
                }
            }
            BasicHeader basicHeader=new BasicHeader("Cookie",s);
            String accountStr = HttpUtils.post(ACCOUNT_INFO, null, httpContext);
            if (StringUtils.isEmpty(accountStr)) {
                return new CommonResponse(ResponseStatusEnum.FAIL);
            }
            JSONObject accountJson = JSON.parseObject(accountStr);
            System.out.println(accountJson);
            if ("000000".equals(accountJson.getString("code"))) {
                CommonResponse commonResponse =
                        new CommonResponse(ResponseStatusEnum.SUCCESS);
                commonResponse.setData(
                        accountJson.getJSONObject("data").get("financialAssets"));
                return commonResponse;
            } else {
                return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
            }
        }
        return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
    }
}
